import {Injectable} from '@nestjs/common';
import {InjectModel} from "nestjs-typegoose";
import {Comment} from "@libs/db/models/comment.model";
import {Model} from "mongoose";
import {paramDto} from "@app/common/dto/param.dto";
import {createCommentDto} from "./dto/create-comment.dto";

@Injectable()
export class CommentsService {
  constructor(@InjectModel(Comment) private readonly commentModel: Model<Comment>,) {
  }

  async getComments(query: string) {
    const params = JSON.parse(query) as paramDto
    const comments = await this.commentModel.find().populate('user').where(params.where).sort('-_id').setOptions(params)
    return comments.filter(comm => comm.user)
  }

  async createComment(dto: createCommentDto, user) {
    dto.user = user.id
    return await this.commentModel.create(dto)
  }
}
